import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
import pandas as pd

# 读取数据集
wine = pd.read_csv("wine.csv")
wine_quality = pd.read_csv("wine_quality.csv", sep=';')

# 将数据集的数据和标签拆分开
wine = wine.iloc[:, 1:]
wine_labels = wine.iloc[:, 0]
wine_data = wine.values
wine_quality = wine_quality.iloc[:, :-1]
wine_quality_labels = wine_quality.iloc[:, -1]
wine_quality_data = wine_quality.values
print("\nwine数据集的数据：")
print(wine_data)
print("\nwine_quality数据集的数据：")
print(wine_quality_data)
print("\nwine数据集的标签")
print(wine_labels)
print("\nwine_quality数据集的标签")
print(wine_quality_labels)

# 将数据集划分为训练集和测试集 测试集比例为10%  (test_size表示测试集对数据集的占比  random_state确保实验结果可复现)
wine_data_train, wine_data_test, wine_labels_train, wine_labels_test = train_test_split(wine_data, wine_labels,
                                                                                        test_size=0.1, random_state=42)
wine_quality_data_train, wine_quality_data_test, wine_quality_labels_train, wine_quality_labels_test = train_test_split(
    wine_quality_data, wine_quality_labels, test_size=0.1, random_state=42)
print("\nwine数据集的训练集：")
print(wine_data_train)
print("\nwine_quality数据集的训练集：")
print(wine_quality_data_train)

# 标准化数据集 对wine数据集采用标准差标准化
wine_data_train_df = pd.DataFrame(wine_data_train)
scaler = StandardScaler()
wine_train_standardized = scaler.fit_transform(wine_data_train_df)
wine_data_test_df = pd.DataFrame(wine_data_test)
scaler = StandardScaler()
wine_test_standardized = scaler.fit_transform(wine_data_test_df)
print("\nwine数据训练集的标准差标准化结果：")
print(wine_train_standardized)
print("\nwine数据测试集的标准差标准化结果：")
print(wine_test_standardized)

# 标准化数据集 对wine_quality数据集采用标准差标准化
wine_quality_data_test_df = pd.DataFrame(wine_quality_data_test)
scaler = StandardScaler()
wine_quality_test_standardized = scaler.fit_transform(wine_quality_data_test_df)
wine_quality_data_train_df = pd.DataFrame(wine_quality_data_train)
scaler = StandardScaler()
wine_quality_train_standardized = scaler.fit_transform(wine_quality_data_train_df)
print("\nwine_quality数据训练集的标准差标准化结果：")
print(wine_quality_train_standardized)
print("\nwine_quality数据测试集的标准差标准化结果：")
print(wine_quality_test_standardized)

####################################################
# 查看每个主成分解释的方差比例
pca = PCA()
transformed_data = pca.fit_transform(wine_train_standardized)
variances = pca.explained_variance_ratio_
print("每个主成分解释的方差比例: ", variances)
######################################################

# PCA降维 n_components表示保留前五个方差最大的特征向量
# 对wine数据集进行PCA降维  ！！前五个特征变量！！
pca = PCA(n_components=5).fit(wine_train_standardized)
wine_trainPCA = pca.transform(wine_train_standardized)
wine_testPCA = pca.transform(wine_test_standardized)
print("\nwine数据训练集的PCA降维结果：")
print(wine_trainPCA)
er = pca.explained_variance_ratio_
print("\nwine数据训练集的PCA降维后各特征方差贡献率：")
print(er)
print("\nwine数据测试集的PCA降维结果：")
print(wine_testPCA)

# 对wine_quality数据集进行PCA降维
pca = PCA(n_components=5).fit(wine_quality_train_standardized)
wine_quality_trainPCA = pca.transform(wine_quality_train_standardized)
wine_quality_testPCA = pca.transform(wine_quality_test_standardized)
print("\nwine_quality数据训练集的PCA降维结果：")
print(wine_quality_trainPCA)
print("\nwine_quality数据测试集的PCA降维结果：")
print(wine_quality_testPCA)

# ##############非必要参考数据######################
# wine_quality_trainPCA = wine_quality_trainPCA.round(2)
# wine_trainPCA = wine_trainPCA.round(2)
# print("格式化数据后(保留两位小数)")
# print("wine_trainPCA")
# print(wine_trainPCA)
# print("wine_quality_trainPCA")
# print(wine_quality_trainPCA)  # 格式化数据
# ##############非必要参考数据######################


# 评价模型性能
from sklearn.linear_model import LinearRegression

clf = LinearRegression().fit(winequality_trainPca, winequality_target_train)
y_pred = clf.predict(winequality_testPca)
print('线性回归模型预测前10个结果为：', '\n', y_pred[:10])

# (2)根据wine_quality数据集处理的结果,构建梯度提升回归模型。
# from sklearn.ensemble import GradientBoostingRegressor
# GBR_wine = GradientBoostingRegressor().\
# fit(winequality_trainPca,winequality_target_train)
# wine_target_pred = GBR_wine.predict(winequality_testPca)
# print('梯度提升回归模型预测前10个结果为：','\n',wine_target_pred[:10])
# print('真实标签前十个预测结果为：','\n',list(winequality_target_test[:10]))

# #(3)结合真实评分和预测评分,计算均方误差、中值绝对误差、可解释方差值。
# #(4)根据得分,判定模型的性能优劣
# print('线性回归模型评价结果：')
# print('winequality数据线性回归模型的平均绝对误差为：',
#      mean_absolute_error(winequality_target_test,y_pred))
# print('winequality数据线性回归模型的均方误差为：',
#      mean_squared_error(winequality_target_test,y_pred))
# print('winequality数据线性回归模型的中值绝对误差为：',
#      median_absolute_error(winequality_target_test,y_pred))
# print('winequality数据线性回归模型的可解释方差值为：',
#      explained_variance_score(winequality_target_test,y_pred))
# print('winequality数据线性回归模型的R方值为：',
#      r2_score(winequality_target_test,y_pred))

# print('梯度提升回归模型评价结果：')
# from sklearn.metrics import explained_variance_score,\
# mean_absolute_error,mean_squared_error,median_absolute_error,r2_score
# print('winequality数据梯度提升回归树模型的平均绝对误差为：',
#      mean_absolute_error(winequality_target_test,wine_target_pred))
# print('winequality数据梯度提升回归树模型的均方误差为：',
#      mean_squared_error(winequality_target_test,wine_target_pred))
# print('winequality数据梯度提升回归树模型的中值绝对误差为：',
#      median_absolute_error(winequality_target_test,wine_target_pred))
# print('winequality数据梯度提升回归树模型的可解释方差值为：',
#      explained_variance_score(winequality_target_test,wine_target_pred))
# print('winequality数据梯度提升回归树模型的R方值为：',
#      r2_score(winequality_target_test,wine_target_pred))
